<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="持有人姓名" prop="type">
        <el-input v-model="queryParams.nickName" placeholder="请输入持有人姓名" clearable style="width: 240px" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd">新建印章</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:article:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :search="false"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange" :row-key="getRowKey">
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
      <el-table-column label="印章名" align="center" prop="name" />
      <el-table-column label="印章图片" align="center" prop="content" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <img :src="basUrlImg + i" v-for="(i, index) in scope.row.signList" width="100" height="100"></img>
        </template>
      </el-table-column>
      <el-table-column label="发送时间" align="center" prop="createTime" />
      <el-table-column label="是否默认" align="center" prop="defaultSign">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.defaultSign" active-color="#1F8AFF" inactive-color="#BDBDBD"
            @change="isdefault(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="持有人姓名" align="center" prop="nickName" />
      <el-table-column label="持有人账号" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-finished" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改印章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="印章名" prop="name">
          <el-input v-model="form.name" placeholder="请输入印章名" />
        </el-form-item>
        <el-form-item label="图片" prop="signUrl">
          <Image-upload v-model="form.signUrl" :limit="1" :is-show-tip="false" not-chinese />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  messageMsgUser,
  signList,
  signAdd,
  signEdit,
  signDelete,
  setDefaultSign
} from "@/api/message/index";

export default {
  name: "Article",
  data() {
    return {
      // 遮罩层
      loading: true,
      basUrlImg: process.env.VUE_APP_BASE_API,
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总持有人账号
      total: 0,
      // 协议表格数据
      articleList: [],
      // 弹出层印章名
      title: "",
      // 是否显示弹出层
      open: false,
      openPerson: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 2
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    isdefault(i) {
      setDefaultSign({
        defaultSign: i.defaultSign,
        id: i.id,
        type: 2
      }).then(res => {
        this.getList()
      })
      console.log(i, 'eeeeee')
    },
    getRowKey(row) {
      return row.id;
    },
    /** 查询协议列表 */
    getList() {
      this.loading = true;
      signList(this.queryParams).then((response) => {
        this.articleList = response.rows.map(item => {
          return {
            ...item,
            signList: item.signUrl.split(',')
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        type: 2
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.nickName = ''
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      // console.log(selection, 'selection.length')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加印章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row
      this.open = true;
      this.title = "修改印章";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            if (this.form.name == "" || this.form.name == null) {
              return this.$modal.msgError("印章名不能为空");
            } else if (this.form.signUrl == null || this.form.signUrl == '') {
              return this.$modal.msgError("请选择图片");
            } else {
              let editInfo = {
                id: this.form.id,
                name: this.form.name,
                signUrl: this.form.signUrl,
                type: 2
              }
              signEdit(editInfo).then((response) => {
                if (response.code == 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.$modal.msgError(response.msg);
                }
              });
            }
          } else {
            if (this.form.name == "" || this.form.name == null) {
              return this.$modal.msgError("印章名不能为空");
            } else if (this.form.signUrl == null || this.form.signUrl == '') {
              return this.$modal.msgError("请选择图片");
            } else {
              signAdd(this.form).then((response) => {
                if (response.code == 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.$modal.msgError(response.msg);
                }

              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除签章信息？')
        .then(function () {
          return signDelete(ids);
        })
        .then(() => {
          this.resetQuery()
          this.multiple = !this.multiple
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/article/export", {
        ...this.queryParams,
      },
        `article_${new Date().getTime()}.xlsx`
      );
    },
  },
};

</script>
<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-tooltip__popper {
  max-width: 50%;
}

.peopleBox {
  border: 1px solid gainsboro;
  border-radius: 4px;
  margin: 10px 0;

  .people_item {
    margin: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
